package com.icss.clothes.cotroller;

import com.icss.clothes.entity.Buy;
import com.icss.clothes.entity.Collection;
import com.icss.clothes.entity.User;
import com.icss.clothes.service.imp.BuyService;
import com.icss.clothes.service.imp.WishlistService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class WishlistController {
    //用于实现跳转到收藏页面的控制器
    @Autowired
    private WishlistService wishlistService;
    @Autowired
    private BuyService buyService;
    //添加收藏
    @RequestMapping("/addWishlist")
    public ModelAndView addCollection(Buy buy, HttpSession session){
        ModelAndView modelAndView = new ModelAndView("forward:/lookWishlist");
        User user = (User) session.getAttribute("loginUser");
        if (user==null) {
            modelAndView.setViewName("login");
            modelAndView.addObject("msg", "请先登录！");
            return modelAndView;
        }
        buy.setUser_id(user.getUser_id());
        String msg = wishlistService.addCollection(buy);
        modelAndView.addObject("msg",msg);
        return modelAndView;
    }
    //查看收藏
    @RequestMapping("/lookWishlist")
    public ModelAndView lookCollection( HttpSession session){
        ModelAndView modelAndView = new ModelAndView("wishlist");
        User user = (User) session.getAttribute("loginUser");
        if (user==null) {
            modelAndView.setViewName("login");
            modelAndView.addObject("msg", "请先登录！");
            return modelAndView;
        }
        List<Buy> collectionList = wishlistService.lookCollection(user.getUser_id());
        modelAndView.addObject("collectionList",collectionList);
        return modelAndView;
    }
    //删除收藏
    @RequestMapping("/deleteWishlist")
   public ModelAndView deleteCollection(int c_id,HttpSession session){
        ModelAndView modelAndView = new ModelAndView("wishlist");
       User user = (User) session.getAttribute("loginUser");
        wishlistService.deleteCollection(c_id,user.getUser_id());
        return modelAndView;
   }
}
